//
//  RegisterViewController.m
//  UI - 豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RegisterViewController.h"
#import "DateBaseSingleton.h"

@interface RegisterViewController ()
@property(nonatomic, retain)UILabel *lable;
@property(nonatomic, retain)UITextField *textField1;
@property(nonatomic, retain)UITextField *textField2;
@property(nonatomic, retain)UITextField *textField3;
@property(nonatomic, retain)UITextField *textField4;
@property(nonatomic, retain)UITextField *textField5;




@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[DateBaseSingleton shareDataBase]createRegisterTable];
    self.view.backgroundColor = [UIColor whiteColor];
    self.lable = [[UILabel alloc] initWithFrame:CGRectMake(40, 100, 80, 20)];
    self.lable.font = [UIFont systemFontOfSize:16];
    self.lable.text = @"用户名：";
    [self.view addSubview:self.lable];
    [self.lable release];
    
    self.textField1 = [[UITextField alloc] initWithFrame:CGRectMake(170, 100, 180, 30)];
    
    self.textField1.layer.borderColor = [UIColor grayColor].CGColor;
    self.textField1.layer.borderWidth = 1.0;
    self.textField1.placeholder = @"请输入用户名";
    self.textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textField1.layer.cornerRadius = 15 / 2;
    [self.view addSubview:self.textField1];
    [self.textField1 release];
    
    
    self.lable = [[UILabel alloc] initWithFrame:CGRectMake(40, 160, 80, 20)];
    self.lable.text = @"密码：";
    self.lable.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.lable];
    [self.lable release];
    
    self.textField2 = [[UITextField alloc] initWithFrame:CGRectMake(170, 160, 180, 30)];
    self.textField2.layer.borderColor = [UIColor grayColor].CGColor;
    self.textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textField2.secureTextEntry = YES;
    self.textField2.placeholder = @"请输入密码";
    self.textField2.layer.borderWidth = 1.0;
    self.textField2.layer.cornerRadius = 15 / 2;
    [self.view addSubview:self.textField2];
    [self.textField2 release];
    
    
    self.lable = [[UILabel alloc] initWithFrame:CGRectMake(40, 220, 80, 20)];
    self.lable.text = @"确认密码：";
    self.lable.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.lable];
    [self.lable release];
    
    self.textField3 = [[UITextField alloc] initWithFrame:CGRectMake(170, 220, 180, 30)];
    self.textField3.placeholder = @"请确认密码";
    self.textField3.layer.borderWidth = 1.0;
    self.textField3.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textField3.secureTextEntry = YES;
    self.textField3.layer.borderColor = [UIColor grayColor].CGColor;
    self.textField3.layer.cornerRadius = 15/2;
    [self.view addSubview:self.textField3];
    [self.textField3 release];
    
    
    self.lable = [[UILabel alloc] initWithFrame:CGRectMake(40, 280, 80, 20)];
    self.lable.text = @"手机号：";
    self.lable.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.lable];
    [self.lable release];
    
    self.textField4 = [[UITextField alloc] initWithFrame:CGRectMake(170, 280, 180, 30)];
    self.textField4.placeholder = @"请输入手机号";
    self.textField4.layer.borderColor = [UIColor grayColor].CGColor;
    self.textField4.keyboardType = UIKeyboardTypeNumberPad;
    self.textField4.layer.cornerRadius = 15 / 2;
    self.textField4.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textField4.layer.borderWidth = 1.0;
    [self.view addSubview:self.textField4];
    [self.textField4 release];
    
    
    self.lable = [[UILabel alloc] initWithFrame:CGRectMake(40, 340, 80, 20)];
    self.lable.text = @"邮箱：";
    self.lable.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.lable];
    [self.lable release];
    
    self.textField5 = [[UITextField alloc] initWithFrame:CGRectMake(170, 340, 180, 30)];
    self.textField5.layer.borderColor = [UIColor grayColor].CGColor;
    self.textField5.layer.cornerRadius = 15 / 2;
    self.textField5.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.textField5.layer.borderWidth = 1.0;
    self.textField5.placeholder = @"请输入邮箱";
    [self.view addSubview:self.textField5];
    [self.textField5 release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
    
    
   
}
- (void)rightAction: (UIBarButtonItem *)btn{
    
    NSMutableArray *array = [[DateBaseSingleton shareDataBase] selectUser];
    
    if (self.textField1.text.length == 0 || self.textField2.text.length == 0) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            
            
            
        }];
        [alertC addAction:action];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        
        
    }
    
        if ([array containsObject:self.textField1.text]) {
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"此用户已经注册过了" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                
            }];
            [alertC addAction:action];
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
        }
    
    if (self.textField2.text != self.textField3.text) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次输入密码不一致" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            
        }];
        [alertC addAction:action];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
    }
    else{
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"恭喜你" message:@"注册成功" preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                [[DateBaseSingleton shareDataBase] insertRegister:self.textField1.text passWord:self.textField2.text confilmPassWord:self.textField3.text phoneNum:self.textField4.text e_mail:self.textField5.text];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }];
            [alertC addAction:action];
            
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
            
        
            
        }
    
    

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
